package com.cmdi.opensite.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cmdi.opensite.entity.Registration;
import com.cmdi.opensite.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名控制器
 */
@RestController
@RequestMapping("/api/registrations")
public class RegistrationController {
    
    @Autowired
    private RegistrationService registrationService;
    
    /**
     * 报名活动
     */
    @PostMapping
    public ResponseEntity<?> registerActivity(@RequestBody Map<String, Long> registrationData, @RequestAttribute("userId") Long userId) {
        Long activityId = registrationData.get("activityId");
        boolean success = registrationService.registerActivity(activityId, userId);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "报名成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "报名失败，可能已报名或活动已满");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 取消报名
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> cancelRegistration(@PathVariable Long id, @RequestAttribute("userId") Long userId) {
        boolean success = registrationService.cancelRegistration(id, userId);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "取消成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "取消失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 查询活动的报名记录（活动组织者接口）
     */
    @GetMapping("/activity/{activityId}")
    public ResponseEntity<?> getActivityRegistrations(@PathVariable Long activityId,
                                                   @RequestParam(defaultValue = "1") Integer current,
                                                   @RequestParam(defaultValue = "10") Integer size) {
        IPage<Registration> page = registrationService.getActivityRegistrations(
            new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), activityId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", page.getRecords());
        result.put("total", page.getTotal());
        result.put("current", current);
        result.put("size", size);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 查询用户的报名记录
     */
    @GetMapping("/user")
    public ResponseEntity<?> getUserRegistrations(@RequestParam(defaultValue = "1") Integer current,
                                               @RequestParam(defaultValue = "10") Integer size,
                                               @RequestAttribute("userId") Long userId) {
        IPage<Registration> page = registrationService.getUserRegistrations(
            new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), userId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", page.getRecords());
        result.put("total", page.getTotal());
        result.put("current", current);
        result.put("size", size);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 更新报名状态（活动组织者接口）
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<?> updateStatus(@PathVariable Long id, @RequestBody Map<String, Integer> statusData) {
        Integer status = statusData.get("status");
        boolean success = registrationService.updateRegistrationStatus(id, status);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "状态更新成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "状态更新失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 批量更新报名状态（活动组织者接口）
     */
    @PutMapping("/batch-status")
    public ResponseEntity<?> batchUpdateStatus(@RequestBody Map<String, Object> batchData) {
        List<Long> ids = (List<Long>) batchData.get("ids");
        Integer status = (Integer) batchData.get("status");
        boolean success = registrationService.batchUpdateStatus(ids, status);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "批量更新成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "批量更新失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 活动签到（活动组织者接口）
     */
    @PutMapping("/{id}/check-in")
    public ResponseEntity<?> checkIn(@PathVariable Long id) {
        boolean success = registrationService.checkIn(id);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "签到成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "签到失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 检查是否已报名
     */
    @GetMapping("/check/{activityId}")
    public ResponseEntity<?> checkRegistration(@PathVariable Long activityId, @RequestAttribute("userId") Long userId) {
        boolean registered = registrationService.hasRegistered(activityId, userId);
        Map<String, Object> result = new HashMap<>();
            result.put("registered", registered);
            return ResponseEntity.ok(result);
    }
}